package com.lagou.edu.controller;

import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.pojo.UserInfo;
import com.lagou.edu.service.LagouTokenService;
import com.lagou.edu.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.UUID;
//      /user/register/{email}/{password}/{code}
//      /user/isRegistered/{email}
//      /user/login/{email}/{password}
//      /user/info/{token}

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private LagouTokenService lagouTokenService;



    @Value("${server.port}")
    private Integer port;

    //注册账号   注册接⼝，true成功，false失败
    @GetMapping("/register/{email}/{password}/{code}")
    public String register(@PathVariable String email,@PathVariable String password,@PathVariable String code) throws Exception {




        UserInfo userInfo = new UserInfo();
        userInfo.setEmail(email);
        userInfo.setPassword(password);
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(UUID.randomUUID().toString());
        lagouToken.setEmail(email);
        lagouTokenService.saveToken(lagouToken);
        return "登录成功！|"+ lagouToken.getToken()+"|"+lagouToken.getEmail()+"|"+userInfoService.addUserInfo(userInfo,code);

    }

    //是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {

        UserInfo userInfos = userInfoService.findUserInfo(email);

        if (userInfos!=null){
            return true;
        }else {
            return false;
        }

    }

    //登录接⼝，验证⽤户名密码合法性，根据⽤户名和密码⽣成token，token存⼊数据库，并写⼊cookie中，登录成功返回邮箱地址，重定向到欢迎⻚
    @GetMapping("/login/{email}/{password}")
    public Object login(@PathVariable String email, @PathVariable String password) throws Exception {

        UserInfo userInfo = userInfoService.findUserInfo(email);

        if (userInfo.getPassword().equals(password)){

            LagouToken lagouToken = new LagouToken();
            lagouToken.setToken(UUID.randomUUID().toString());
            lagouToken.setEmail(email);
            lagouTokenService.saveToken(lagouToken);
            return "登录成功！|"+ lagouToken.getToken()+"|"+lagouToken.getEmail();

//            Result result = new Result();
//            result.setB(true);
//            result.setMessage("登录成功！"+ lagouToken.getToken());
//            return result;
        }else {
//            Result result = new Result();
//            result.setB(false);
//            result.setMessage("登录失败，邮箱或密码输入错误");
//            return result;
            return false;
        }

    }


//     根据token查询⽤户登录邮箱接⼝

    @GetMapping("/info/{token}")
    public String info(@PathVariable String token ) throws Exception {


        String mail = lagouTokenService.findMailByToken(token);

        if (mail!=null){
            return mail;
        }else {
            return "输入错误";
        }

    }
}
